﻿using _8Group.MES.ClientManagement;
using _8Group.MES.MeasuringUnit;
using _8Group.MES.Meng_Service;
using _8Group.MES.MengIService.DTO;
using _8Group.MES.Model.MD主数据;
using _8Group.MES.Service;
using _8Group.MES.SongService;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System;
using _8Group.MES.MengService;
using _8Group.MES.MengIService;

namespace _8Group.MES.Web.Controllers
{
    /// <summary>
    /// 孟祥宇控制器(个人任务的所有视图和方法都写在该控制器里)
    /// </summary>
    public class MengController : Controller
    {
        private readonly MdItemTypeService _mdItemTypeService;
        private readonly MeasuringUnitService _measuringUnitService;
        private readonly MaterialProductService _materialProductService;
        private readonly MdVendorService _mdvendorService;
        private readonly ClientAppServic _mdClientService;
        private readonly StockService _stockService;
        private readonly WmRtIssueService _rtIssueService;
        private readonly WmTransferService _wmTransferService;
        private readonly OutSousceService _wmOutSourceService;
        private readonly DvCheckSubjectService _dvCheckSubjectService;

        public MengController(MdItemTypeService mdItemTypeService, MeasuringUnitService measuringUnitService, MaterialProductService materialProductService, MdVendorService mdvendorService, ClientAppServic mdClientService, StockService stockService,WmRtIssueService rtIssueService, WmTransferService wmTransferService, OutSousceService wmOutSourceService, DvCheckSubjectService dvCheckSubjectService)
        {
            _mdItemTypeService = mdItemTypeService;
            _measuringUnitService = measuringUnitService;
            _materialProductService = materialProductService;
            _mdvendorService = mdvendorService;
            _mdClientService = mdClientService;
            _stockService = stockService;
            _rtIssueService = rtIssueService;
            _wmTransferService = wmTransferService;
            _wmOutSourceService = wmOutSourceService;
            _dvCheckSubjectService = dvCheckSubjectService;
        }

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 添加物料产品
        /// </summary>
        /// <returns></returns>
        public IActionResult AddMdItemView()
        {
            return View();
        }
        /// <summary>
        /// 修改物料产品
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateMdItemView()
        {
            return View();
        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <returns></returns>
        public IActionResult AddMdVendorView()
        {
            return View();
        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateMdVendorView()
        {
            return View();
        }

        /// <summary>
        /// 库存现有量显示
        /// </summary>
        /// <returns></returns>
        public IActionResult StockView()
        {
            return View();
        }
        /// <summary>
        /// 添加库存现有量
        /// </summary>
        /// <returns></returns>
        public IActionResult AddStockView()
        {
            return View();
        }
        /// <summary>
        /// 修改库存现有量显示
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateStockView()
        {
            return View();
        }

        /// <summary>
        /// 生产退料显示
        /// </summary>
        /// <returns></returns>
        public IActionResult OutSourceView()
        {
            return View();
        }
        /// <summary>
        /// 添加生产退料
        /// </summary>
        /// <returns></returns>
        public IActionResult AddOutSourceView()
        {
            return View();
        }
        /// <summary>
        /// 修改生产退料
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateOutSourceView()
        {
            return View();
        }
        /// <summary>
        /// 转移调拨列表
        /// </summary>
        /// <returns></returns>
        public IActionResult WmTransferView()
        {
            return View();
        }
        /// <summary>
        /// 转移调拨添加
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWmTransferView()
        {
            return View();
        }
        /// <summary>
        /// 转移调拨修改
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateWmTransferView()
        {
            return View();
        }

        /// <summary>
        /// 外协发料列表
        /// </summary>
        /// <returns></returns>
        public IActionResult WmOutSorsceView()
        {
            return View();
        }
        /// <summary>
        /// 外协发料添加
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWmOutSorsceView()
        {
            return View();
        }
        /// <summary>
        /// 外协发料修改
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateWmOutSorsceView()
        {
            return View();
        }
        /// <summary>
        /// 点检保养项目列表
        /// </summary>
        /// <returns></returns>
        public IActionResult DvCheckSubjectView()
        {
            return View();
        }
        /// <summary>
        /// 点检保养项目添加
        /// </summary>
        /// <returns></returns>
        public IActionResult AddDvCheckSubjectView()
        {
            return View();
        }
        /// <summary>
        /// 点检保养项目修改
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateDvCheckSubjectView()
        {
            return View();
        }





        /// <summary>
        /// 物料产品添加
        /// </summary>
        /// <param name="mdItem"></param>
        /// <returns></returns>
        public async Task<int> AddMdItemMethod(string mdItem)
        {
            var itemDto=JsonConvert.DeserializeObject<MdItemDTO>(mdItem);
            itemDto.ItemTypeCode = "123";
            itemDto.ItemTypeId = 123;
            itemDto.ItemTypeName = "123";
            itemDto.MaxStock = 123;
            itemDto.MinStock = 23;
            itemDto.CreateBy = "孟祥宇";
            itemDto.CreateTime = DateTime.Now;

            var n = await _materialProductService.AddMdItemAsync(itemDto);
            return 1;
        }
        /// <summary>
        /// 物料产品修改
        /// </summary>
        /// <param name="mdItem"></param>
        /// <returns></returns>
        public async Task<int> UpdateMdItem(UpdateMdItemDTO mdItem)
        {
            var n=await _materialProductService.UpdateMdItemAsync(mdItem);
            return 1;
        }

        /// <summary>
        /// 物料产品删除
        /// </summary>
        /// <param name="mdItem"></param>
        /// <returns></returns>
        public async Task<int> DeleteMdItem(int id)
        {
            var n = await _materialProductService.DeleteMdItemAsync(id);
            return 1;
        }

        /// <summary>
        /// 供应商新增
        /// </summary>
        /// <param name="mdvendor"></param>
        /// <returns></returns>
        public async Task<int> AddMdVendor(string mdVendor)
        {
            var mdvendorDTO = JsonConvert.DeserializeObject<MdVendorDTO>(mdVendor);
            mdvendorDTO.CreateBy = "孟祥宇";
            mdvendorDTO.CreateTime = DateTime.Now;
            var n = await _mdvendorService.AddMdVendorAsync(mdvendorDTO);

            return 1;
        }

        /// <summary>
        /// 供应商修改
        /// </summary>
        /// <param name="mdItem"></param>
        /// <returns></returns>
        public async Task<int> UpdateMdVendor(UpdateMdVendorDTO mdVendor)
        {
            var n = await _mdvendorService.UpdateMdVendorAsync(mdVendor);
            return 1;
        }

        /// <summary>
        /// 供应商删除
        /// </summary>
        /// <param name="mdItem"></param>
        /// <returns></returns>
        public async Task<int> DeletMdVendor(int id)
        {
            var n = await _mdvendorService.DeleteMdVendorAsync(id);
            return 1;
        }

        /// <summary>
        /// 库存现有量
        /// </summary>
        /// <param name="itemCode"></param>
        /// <param name="itemName"></param>
        /// <param name="batchCode"></param>
        /// <param name="wareHouseName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<string> GetStock(string? itemCode, string? itemName, string? batchCode, string? wareHouseName, int page, int limit)
        {
            var stocks = await _stockService.GetStockAsync(itemCode, itemName, batchCode, wareHouseName, page, limit);
            return stocks;
        }
        /// <summary>
        /// 库存现有量新增
        /// </summary>
        /// <param name="stock"></param>
        /// <returns></returns>
        public async Task<int> AddStock(string stock)
        {
            var stockDTO = JsonConvert.DeserializeObject<StockDTO>(stock);
            stockDTO.CreateBy = "孟祥宇";
            stockDTO.CreateTime = DateTime.Now;
            var n = await _stockService.AddStockAsync(stockDTO);

            return 1;
        }
        /// <summary>
        /// 库存现有量修改
        /// </summary>
        /// <param name="mdItem"></param>
        /// <returns></returns>
        public async Task<int> UpdateStock(UpdateStockDTO stock)
        {
            stock.UpdateBy = "孟祥宇";
            stock.UpdateTime = DateTime.Now;
            var n = await _stockService.UpdateStockAsync(stock);
            return 1;
        }
        /// <summary>
        /// 库存现有量删除
        /// </summary>
        /// <param name="mdItem"></param>
        /// <returns></returns>
        public async Task<int> DeletStock(int id)
        {
            var n = await _stockService.DeleteStockAsync(id);
            return 1;
        }
        /// <summary>
        /// 获取退料列表
        /// </summary>
        /// <param name="itemCode"></param>
        /// <param name="batchCode"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<object> GetRtIssue(string? itemCode, string? batchCode, string? wareHouseName, int page, int limit)
        {
            var rtIssue = await _rtIssueService.GetRtIssueAsync(itemCode, batchCode, wareHouseName, page, limit);
            return rtIssue;
        }
        /// <summary>
        /// 退料添加
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> AddRtIssue(string WmRtIssue)
        {
            var WmRtIssueDTO = JsonConvert.DeserializeObject<WmRtIssueDTO>(WmRtIssue);
            WmRtIssueDTO.CreateBy = "孟祥宇";
            WmRtIssueDTO.CreateTime = DateTime.Now;
            var n = await _rtIssueService.AddRtIssueAsync(WmRtIssueDTO);
            return 1;
        }
        /// <summary>
        /// 退料修改
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> UpdateRtIssue(UpdateWmRtIssueDTO WmRtIssue)
        {
            WmRtIssue.UpdateBy = "孟祥宇";
            WmRtIssue.UpdateTime = DateTime.Now;
            var n = await _rtIssueService.UpdateRtIssueAsync(WmRtIssue);
            return 1;
        }
        /// <summary>
        /// 退料删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteRtIssue(int id)
        {
            var n = await _rtIssueService.DeleteRtIssueAsync(id);
            return 1;
        }
        /// <summary>
        /// 获取转移调拨列表
        /// </summary>
        /// <param name="transferCode"></param>
        /// <param name="transferName"></param>
        /// <param name="transferType"></param>
        /// <param name="fromWarehouseName"></param>
        /// <param name="toWarehouseName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<string> GetTransfer(string? transferCode, string? transferName, string? transferType, string? fromWarehouseName, string? toWarehouseName, int page, int limit)
        {
            var transfers = await _wmTransferService.GetTransferAsync(transferCode, transferName, transferType, fromWarehouseName, toWarehouseName, page, limit);
            return transfers;
        }
        /// <summary>
        /// 转移调拨添加
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> AddTransfer(string wmTransfer)
        {
            var WmTransferDTO = JsonConvert.DeserializeObject<WmTransferDTO>(wmTransfer);
            WmTransferDTO.CreateBy = "孟祥宇";
            WmTransferDTO.CreateTime = DateTime.Now;
            var n = await _wmTransferService.AddTransferAsync(WmTransferDTO);
            return 1;
        }
        /// <summary>
        /// 转移调拨修改
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> UpdateTransfer(UpdateWmTransferDTO WmTransfer)
        {
            WmTransfer.UpdateBy = "孟祥宇";
            WmTransfer.UpdateTime = DateTime.Now;
            var n = await _wmTransferService.UpdateTransferAsync(WmTransfer);
            return 1;
        }
        /// <summary>
        /// 转移调拨删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteTransfer(int id)
        {
            var n = await _wmTransferService.DeleteTransferAsync(id);
            return 1;
        }

        /// <summary>
        /// 获取外协发料列表
        /// </summary>
        /// <param name="issueCode"></param>
        /// <param name="workorderCode"></param>
        /// <param name="vendorName"></param>
        /// <param name="issueDate"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<string> GetOutSource(string? issueCode, string? workorderCode, string? vendorName, string? issueDate, int page, int limit)
        {
            var transfers = await _wmOutSourceService.GetOutSourceAsync(issueCode, workorderCode, vendorName, issueDate, page, limit);
            return transfers;
        }
        /// <summary>
        /// 外协发料添加
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> AddOutSource(string outSource)
        {
            var OutSourceDTO = JsonConvert.DeserializeObject<OutSourceDTO>(outSource);
            OutSourceDTO.CreateBy = "孟祥宇";
            OutSourceDTO.CreateTime = DateTime.Now;
            var n = await _wmOutSourceService.AddOutSourceAsync(OutSourceDTO);
            return 1;
        }
        /// <summary>
        /// 外协发料修改
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> UpdateOutSource(UpdateOutSourceDTO outSource)
        {
            outSource.UpdateBy = "孟祥宇";
            outSource.UpdateTime = DateTime.Now;
            var n = await _wmOutSourceService.UpdateOutSourceAsync(outSource);
            return 1;
        }
        /// <summary>
        /// 外协发料删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteOutSource(int id)
        {
            var n = await _wmOutSourceService.DeleteOutSourceAsync(id);
            return 1;
        }
        /// <summary>
        /// 点检保养项目列表
        /// </summary>
        /// <param name="subjectCode"></param>
        /// <param name="subjectName"></param>
        /// <param name="subjectType"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<string> GetCheckSubject(string? subjectCode, string? subjectName, string? subjectType, int page, int limit)
        {
            var transfers = await _dvCheckSubjectService.GetCheckSubjectAsync(subjectCode, subjectName, subjectType, page, limit);
            return transfers;
        }
        /// <summary>
        /// 点检保养项目添加
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> AddCheckSubject(string checkSubject)
        {
            var CheckSubjectDTO = JsonConvert.DeserializeObject<DvCheckSubjectDTO>(checkSubject);
            CheckSubjectDTO.CreateBy = "孟祥宇";
            CheckSubjectDTO.CreateTime = DateTime.Now;
            var n = await _dvCheckSubjectService.AddCheckSubjectAsync(CheckSubjectDTO);
            return 1;
        }
        /// <summary>
        /// 点检保养项目修改
        /// </summary>
        /// <param name="WmRtIssue"></param>
        /// <returns></returns>
        public async Task<int> UpdateCheckSubject(UpdateDvCheckSubjectDTO checkSubject)
        {
            checkSubject.UpdateBy = "孟祥宇";
            checkSubject.UpdateTime = DateTime.Now;
            var n = await _dvCheckSubjectService.UpdateCheckSubjectAsync(checkSubject);
            return 1;
        }
        /// <summary>
        /// 点检保养项目删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteCheckSubject(int id)
        {
            var n = await _dvCheckSubjectService.DeleteCheckSubjectAsync(id);
            return 1;
        }


    }
}
